package com.zd.bms.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import com.zd.bms.bean.vo.ScenicSpotDetailVo;
import com.zd.bms.bean.vo.req.ScenicSpotListReqVo;
import com.zd.bms.service.IScenicSpotService;
import com.zd.common.bean.model.BmsUser;
import com.zd.common.bean.model.ScenicSpot;
import com.zd.common.common.enums.BmsUserTypeEnum;
import com.zd.common.dao.common.Page;

/**
 * 景区管理
 * 
 * @author lishi
 * @date 2018年11月22日 上午10:22:13
 */
@Controller
@RequestMapping("scenicSpot")
public class ScenicSpotController extends BaseController {
	@Autowired
	private IScenicSpotService scenicSpotService;

	@RequestMapping("index")
	public ModelAndView index() throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("scenicSpot/index");
		BmsUser bmsUser = getSessionUser();
		if (BmsUserTypeEnum.Platform_user.getKey() == bmsUser.getType()) {
			// 平台用户
			mv.setViewName("scenicSpot/platformIndex");
		} else if (BmsUserTypeEnum.Scenic_area_administrator.getKey() == bmsUser.getType()) {
			// 景区管理员
			Integer scenicSpotId = bmsUser.getScenicSpotId();
			ScenicSpotDetailVo detail = scenicSpotService.getScenicSpotDetail(scenicSpotId);
			mv.addObject("detail", detail);
		}
		return mv;
	}

	@ResponseBody
	@RequestMapping("list")
	public Map<String, Object> list(ScenicSpotListReqVo reqVo) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		Page<ScenicSpotDetailVo> resultPage = scenicSpotService.pageScenicSpotDetail(reqVo);
		resultMap.put("data", resultPage.getList());
		resultMap.put("draw", reqVo.getDraw());
		resultMap.put("recordsTotal", resultPage.getList().size());
		resultMap.put("recordsFiltered", resultPage.getTotalRow());
		return resultMap;
	}

	@RequestMapping("updateIndex")
	public ModelAndView updateIndex(int scenicSpotId) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("scenicSpot/updateIndex");
		ScenicSpotDetailVo detail = scenicSpotService.getScenicSpotDetail(scenicSpotId);
		mv.addObject("detail", detail);
		return mv;
	}

	@RequestMapping("saveOrUpdateIndex")
	public ModelAndView saveOrUpdateIndex(Integer scenicSpotId) throws Exception {
		ModelAndView mv = new ModelAndView();
		mv.setViewName("scenicSpot/saveOrUpdateIndex");
		if (scenicSpotId != null) {
			ScenicSpotDetailVo detail = scenicSpotService.getScenicSpotDetail(scenicSpotId);
			mv.addObject("detail", detail);
		}
		return mv;
	}

	@ResponseBody
	@RequestMapping("update")
	public Map<String, Object> update(ScenicSpot scenicSpot) throws Exception {
		Map<String, Object> resultMap = new HashMap<>();
		BmsUser bmsUser = getSessionUser();
		Integer scenicSpotId = bmsUser.getScenicSpotId();
		if (scenicSpotId != 0 && scenicSpotId != scenicSpot.getId()) {
			resultMap.put("result", false);
			resultMap.put("errorMsg", "你没有权限更新!");
			return resultMap;
		}
		ScenicSpot originScenicSpot = scenicSpotService.get(scenicSpot.getId());
		BeanUtils.copyProperties(scenicSpot, originScenicSpot, "appId", "accountId", "createTime");
		originScenicSpot.setUpdateTime(new Date());
		scenicSpotService.update(originScenicSpot);
		resultMap.put("result", true);
		return resultMap;
	}

	@ResponseBody
	@RequestMapping("saveOrUpdate")
	public Map<String, Object> saveOrUpdate(ScenicSpotDetailVo detail) throws Exception {
		return scenicSpotService.saveOrUpdateScenicSpotDetail(detail);
	}
}
